﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class admin_Admin_AccessCopy : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!Page.IsPostBack)
        {
            this.ShowFile();
        }
        this.UserList.Attributes.Add("bordercolor", "#B8D3F1");
    }
    private void ShowFile()
    {

        DataTable dt = new DataTable();
        dt.Columns.Add("AccessName");
        dt.Columns.Add("AccessTime");
        DirectoryInfo showall = new DirectoryInfo(Request.ServerVariables["APPL_PHYSICAL_PATH"] + "\\Backup\\");
        foreach (FileSystemInfo f in showall.GetFileSystemInfos())
        {
            DataRow dr = dt.NewRow();
            dr["AccessName"] = f.Name;
            dr["AccessTime"] = f.CreationTime;
            dt.Rows.Add(dr);
        }
        UserList.DataSource = dt;
        UserList.DataKeyNames = new string[] { "AccessName" };//主键
        UserList.DataBind();
    }
    protected void UserList_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        //限制标题换行
        e.Row.Cells[2].Attributes.Add("style", "word-break :break-all ; word-wrap:break-word");
        //鼠标换背景色
        int i;
        //执行循环，保证每条数据都可以更新
        for (i = 0; i < UserList.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E6F5FA'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
        //删除提醒框
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要用：\"" + e.Row.Cells[1].Text + "\" 还原数据库吗?')");
            }
        }
    }
    protected void UserList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        UserList.EditIndex = e.NewEditIndex;
        this.ShowFile();
    }
    //还原
    protected void UserList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //要还原的
        string dbPath = Request.ServerVariables["APPL_PHYSICAL_PATH"] + System.Web.Configuration.WebConfigurationManager.ConnectionStrings["AccessPath"].ConnectionString;
        //备份文件
        string bakPath = Request.ServerVariables["APPL_PHYSICAL_PATH"] + "\\Backup\\" + UserList.DataKeys[e.RowIndex].Value.ToString();
        //临时文件
        string tempPath = Request.ServerVariables["APPL_PHYSICAL_PATH"] + "\\Backup\\tmp.tmp";
        if (!File.Exists(bakPath))
        {
            throw new Exception("备份数据库不存在,无法还原");
        }
        try
        {
            File.Copy(bakPath, tempPath, true);
            File.Delete(dbPath);
            File.Move(tempPath, dbPath);
            DB.Msg("还原成功~！");
        }
        catch (IOException iXP)
        {
            throw new Exception(iXP.Message, iXP);
        }
        this.ShowFile();
    }
    //修改文件名
    protected void UserList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string dbPath = Request.ServerVariables["APPL_PHYSICAL_PATH"] + "\\Backup\\" + UserList.DataKeys[e.RowIndex].Value.ToString();
        string upPath = Request.ServerVariables["APPL_PHYSICAL_PATH"] + "\\Backup\\" + ((TextBox)(UserList.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
        File.Move(dbPath,upPath);
        UserList.EditIndex = -1;
        this.ShowFile();
    }
    //取消
    protected void UserList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        UserList.EditIndex = -1;
        this.ShowFile();
    }
    //删除
    protected void Button1_Click(object sender, EventArgs e)
    {

        for (int i = 0; i < UserList.Rows.Count; i++)
        {
            CheckBox cbox = (CheckBox)UserList.Rows[i].Cells[0].FindControl("del");
            if (cbox.Checked == true)
            {
                File.Delete(Request.ServerVariables["APPL_PHYSICAL_PATH"] + "\\Backup\\" + UserList.DataKeys[i].Value.ToString());
            }
        }
        this.ShowFile();
        Button2.Text = "全选";

    }
    //全选
    protected void Button2_Click(object sender, EventArgs e)
    {
        CheckBox cb;
        if (Button2.Text == "全选")
        {
            for (int i = 0; i < UserList.Rows.Count; i++)
            {
                cb = (CheckBox)UserList.Rows[i].Cells[0].FindControl("del");
                cb.Checked = true;
            }
            Button2.Text = "取消";
        }
        else
        {
            for (int i = 0; i < UserList.Rows.Count; i++)
            {
                cb = (CheckBox)UserList.Rows[i].Cells[0].FindControl("del");
                cb.Checked = false;
            }
            Button2.Text = "全选";
        }

    }
    //备份数据库
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (Acopy.Text != "" || Acopy.Text != null)
        {
        string srcPath = Request.ServerVariables["APPL_PHYSICAL_PATH"] + System.Web.Configuration.WebConfigurationManager.ConnectionStrings["AccessPath"].ConnectionString;
        string aimPath = Request.ServerVariables["APPL_PHYSICAL_PATH"] + "\\Backup\\" + Acopy.Text.ToString().Trim() + ".Config";
        if (!File.Exists(srcPath))
        {
            throw new Exception("源数据库不存在,无法备份");
        }
        try
        {
            File.Copy(srcPath, aimPath, true);
            DB.Msg("备份成功");
        }
        catch (IOException iXP)
        {
            throw new Exception(iXP.Message,iXP);
        }

        }
        else
        { DB.Msg("不能为空~！"); }
    }

}
